// pages/authentication/authentication.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api');
const util = require('../../utils/util.js');
const constant = require('../../utils/constant');
const storage = require('../../utils/storage');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURL: constant.BASEURL,
    BASEURLWWW:constant.BASEURLWWW,
    isChecked: false,
    isCameraTipsShow: false, // 是否显示认证提示
    isCameraShow: false, // 是否显示摄像头
    behaviorType: '',
    behavior: '',
    authenticationOption: {
      behaviorToken: '',
      name: '',
      no: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ctx = wx.createCameraContext();
  },
  // 返回上一级
  back:function(){
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },
  // 单选框
  radioChange:function(e){
    let isChecked = this.data.isChecked
    if(isChecked){
      isChecked = false
    }else{
      isChecked = true
    }
    this.setData({
      isChecked
    })
  },
  // 表单提交
  formSubmit:function(e){
    let that = this
    let isChecked = this.data.isChecked
    console.info(e.detail.value)
    if(isChecked){
      if(util.checkEmpty(e.detail.value.name)){
        wx.showToast({
          title: '姓名不能为空！',
          icon: 'none'
        })
        return false
      }
      if(util.checkEmpty(e.detail.value.no)){
        wx.showToast({
          title: '身份证号不能为空！',
          icon: 'none'
        })
        return false
      }
      if(!util.checkIdCard(e.detail.value.no)){
        wx.showToast({
          title: '身份证号不正确！',
          icon: 'none'
        })
        return false
      }
      // 认证第一步
      api.behavior().then(res => {
        console.info(res)
        if(res.code == 0){
          that.setData({
            authenticationOption: {...e.detail.value},
            'authenticationOption.behaviorToken': res.data.data.behaviorToken,
            behaviorType: res.data.data.behaviorType,
            behavior: res.data.data.behavior,
            isCameraTipsShow: true
          })
          console.info(that.data.authenticationOption)
        }
      })
    }
  },
  // 显示摄像
  cameraShow:function(){
    this.setData({
      isCameraShow: true
    })
  },
  //touch start 手指触摸开始
  handleTouchStart:function(e)  {    
    this.startTime  =  e.timeStamp;    
    console.log(" startTime = "  +  e.timeStamp);  
    console.log(" 手指触摸开始 " ,  e);  
    console.log(" this " , this);  
  },
  //touch end 手指触摸结束
  handleTouchEnd: function(e)  {    
    this.endTime  =  e.timeStamp;    
    console.log(" endTime = "  +  e.timeStamp);  
    console.log(" 手指触摸结束 ", e);
    //判断是点击还是长按 点击不做任何事件，长按 触发结束录像
    if (this.endTime - this.startTime > 350) {
      //长按操作 调用结束录像方法
      this.stopShootVideo();
    }
  },
  /**
   * 长按按钮 - 录像
   */
  handleLongPress: function(e)  {
    console.log("endTime - startTime = "  +  (this.endTime  -  this.startTime));
    console.log("长按");
    // 长按方法触发，调用开始录像方法
    this.startShootVideo();
  },
  /**
   * 开始录像的方法
   */
  startShootVideo() {
    console.log("========= 调用开始录像 ===========")
    this.ctx.startRecord({
      success: (res) => {
      },
      fail() {
        console.log("========= 调用开始录像失败 ===========")
      }
    })
  },
  /**
   * 结束录像
   */
  stopShootVideo() {
    let that = this
    console.log("========= 调用结束录像 ===========")
    this.ctx.stopRecord({
      success: (res) => {
        console.info(res)
        that.setData({
          isCameraShow: false
        })
        let { behaviorToken,name,no } = that.data.authenticationOption
        console.info(that.data.authenticationOption)
        wx.uploadFile({
          url: that.data.BASEURL+'/user/verify', //仅为示例，非真实的接口地址
          filePath: res.tempVideoPath,
          name: 'file',
          header:{
            'Authorization': 'Bearer '+ wx.getStorageSync(storage.LOCAL_JWT_KEY),
            'Deviceid': 'loveqing'
          },
          formData: {
            'behaviorToken':behaviorToken,
            'name': name,
            'no': no
          },
          success (res){
            wx.showToast({
              title: '成功！',
              icon:'success',
            })
            setTimeout(()=>{
              wx.reLaunch({
                url: '/pages/index/index',
              })
            },500)
          }
        })
        // that.verify()
        // console.info(that.data.authenticationOption)
      },
      fail() {
        console.log("========= 调用结束录像失败 ===========")
      }
    })
  },
  // 第二步验证
  verify:function(){
    api.verify(this.data.authenticationOption).then(res => {
      console.info(res)
    })
  }
})